package com.ym.jiujiangeyou.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ym.jiujiangeyou.entity.Hotel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 * @Description: hotel数据库操作类
 */
public class HotelService {

	DataBaseHelper helper;
	SQLiteDatabase db;
	String[] columns = new String[] { "hname", "hid", "address",
			"introduction","imageurl", "longitude",
			"latitude", "priceinfo", "phone", "traffic", "facility",
			"price"};
	Context context;
	
	public HotelService(Context context) {
		helper = new DataBaseHelper(context);
		this.context = context;
	}
	
	/** 
	* @param hotel
	* @return  
	* @retur  long 
	* @Description: 保存hotel 信息 
	*/
	public long insetHotelToBase(Hotel hotel) {
		long i = 0;
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("hname", hotel.getName());
		values.put("hid", hotel.getId());
		values.put("address", hotel.getAddress());
		values.put("introduction", hotel.getIntroduction());
		values.put("imageurl", hotel.getImageurl());
		values.put("longitude", hotel.getLongitude());
		values.put("latitude", hotel.getLatitude());
		values.put("priceinfo", hotel.getPriceinfo());
		values.put("phone", hotel.getPhone());
		values.put("traffic", hotel.getTraffic());
		values.put("facility", hotel.getFacility());
		values.put("price", hotel.getPrice());
		i = db.insert("hotel", null, values);
		db.close();
		return i;
	}
	
	/** 
	* @param sname
	* @return  
	* @retur  boolean 
	* @Description: 根据name查询 
	*/
	public boolean queryByName(String hname) {
		boolean flag = false;
		db = helper.getReadableDatabase();
		Cursor cursor = db.query("hotel", columns, "hname = ?",
				new String[] { hname }, null, null, null);
		if (cursor != null && cursor.moveToNext()) {
			flag = true;
		}
		db.close();
		return flag;
	}
	
	/** 
	* @return  
	* @retur  Map<String,Object> 
	* @Description: 查询全部 
	*/
	public Map<String, Object> query() {
		db = helper.getReadableDatabase();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Hotel> hotels = new ArrayList<Hotel>();
		Hotel hotel = null ;
		Cursor cursor = db.query("hotel", columns, null, null, null, null,
				null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				hotel = new Hotel();
				hotel.setId(cursor.getInt(cursor.getColumnIndex("hid")));
				hotel.setName(cursor.getString(cursor.getColumnIndex("hname")));
				hotel.setAddress(cursor.getString(cursor
						.getColumnIndex("address")));
				hotel.setIntroduction(cursor.getString(cursor
						.getColumnIndex("introduction")));
				hotel.setImageurl(cursor.getString(cursor
						.getColumnIndex("imageurl")));
				hotel.setLatitude(cursor.getDouble(cursor
						.getColumnIndex("latitude")));
				hotel.setLongitude(cursor.getDouble(cursor
						.getColumnIndex("longitude")));
				hotel.setPriceinfo(cursor.getString(cursor.getColumnIndex("priceinfo"))) ;
				hotel.setPhone(cursor.getString(cursor
						.getColumnIndex("phone")));
				hotel.setTraffic(cursor.getString(cursor
						.getColumnIndex("traffic")));
				hotel.setFacility(cursor.getString(cursor
						.getColumnIndex("facility")));
				hotel.setPrice(cursor.getString(cursor
						.getColumnIndex("price")));
				hotels.add(hotel);
			}
			map.put("hotel", hotels);
		}
		db.close();
		return map;
	}
	
	/** 
	* @param name  
	* @retur  void 
	* @Description: 根据name删除 
	*/
	public void delByName(String name){
		db = helper.getReadableDatabase();
		String sql = "delete from hotel where hname = ?" ;
		db.execSQL(sql, new Object[]{name}) ;
		db.close() ;
	}
	

}
